# 文件操作模块有以下功能(写在类里面的函数)：
# 读取文件内容
# 写入内容到文件中
# 复制文件
# 删除文件
# 文件改名

import os

class File:

    @staticmethod
    def read(filename):
        f = open(filename, 'r', encoding='utf8')
        content = f.read()
        f.close()
        return content

    @staticmethod
    def append(filename, content):
        f = open(filename, 'a', encoding='utf8')
        f.write(content)
        f.close()
        print('写入成功')

    @staticmethod
    def copy(old_filename):
        # 准备新文件
        filename, extend = os.path.splitext(old_filename)  # demo.txt ->　('demo', '.txt')
        new_filename = filename + '-副本' + extend

        # 打开源文件(rb)
        old_file = open(old_filename, 'rb')
        # 打开新文件(ab)
        new_file = open(new_filename, 'ab')

        # 复制操作
        while True:
            content = old_file.read(1024)  # 1024B -> 1kb
            if not content:
                break
            new_file.write(content)

        # 关闭资源句柄
        old_file.close()
        new_file.close()

    @staticmethod
    def delete(filename):
        os.remove(filename)

    @staticmethod
    def rename(filename, new_filename):
        os.rename(filename, new_filename)


# 查看python关键字
# import keyword
# print(keyword.kwlist)

# 格式化代码-快捷键
# ctrl alt l

filename = 'demo1.txt'
# 写入文件
File.append(filename, 'hello123')
# 读文件
print(File.read(filename))
# 复制文件
File.copy(filename)
# 重命名
File.rename(filename, 'demo_bak.txt')
# 删除
File.delete('demo_bak.txt')






